package com.example.lzy.lzydemo.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.example.lzy.lzydemo.R;
import com.example.lzy.lzydemo.util.DBHelper;

public class DBActivity extends AppCompatActivity {

    SQLiteDatabase db ;
    DBHelper dbHelper;
    private String tbname = "test";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        dbHelper = new DBHelper(DBActivity.this, "test", null, 1);

        db = dbHelper.getWritableDatabase();
        initDatabase(db);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DBHelper dbHelper = new DBHelper(DBActivity.this, tbname, null, 1);
                ContentValues cv = new ContentValues();
                String [] colums = {"fileName","description"};
                String selection = "fileName=?";
                String[] selectionArgs = new String[]{"pic2.jpg"};
                Cursor  c = db.query(tbname, colums, selection, selectionArgs, null, null, null);
                int nameColumn = c.getColumnIndex("fileName");
                int phoneColumn = c.getColumnIndex("description");
                Toast.makeText(DBActivity.this, c.getString(nameColumn)+"/"+c.getString(phoneColumn),Toast.LENGTH_LONG);

            }
        });
    }

    //初始化表
    public void initDatabase(SQLiteDatabase db) {
        ContentValues cv = new ContentValues();

        cv.put("fileName", "pic1.jpg");
        cv.put("description", "图片1");
        db.insert(tbname, "", cv);

        cv.put("fileName", "pic2.jpg");
        cv.put("description", "图片2");
        db.insert(tbname, "", cv);

        cv.put("fileName", "pic3.jpg");
        cv.put("description", "图片3");
        db.insert(tbname, "", cv);

        cv.put("fileName", "pic4.jpg");
        cv.put("description", "图片4");
        db.insert(tbname, "", cv);

    }

}
